/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.pi;

import java.awt.Window;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import researchgrants.parts.db.Db;

/**
 * A class to get the starting list of all grants in a PiList.
 * @author ohad
 */
public class AllPis {

    static PisList allPis;
    static {
       allPis=new PisList();
        try {
            Connection conn=Db.openDbConnection();
            PreparedStatement prepareStatement = Db.createPreparedStatement(conn, "SELECT PiID FROM tblPis");
            prepareStatement.executeQuery();
            ResultSet pisRs = prepareStatement.getResultSet();
            while (pisRs.next()) {
                int PiId=(Integer)Db.getColumn(pisRs, 1);
                allPis.add(Pi.getById(PiId));
            }
            pisRs.close();
            prepareStatement.close();
            Db.closeDbConnection(conn);
        } catch (SQLException ex) {
            Logger.getLogger(AllPis.class.getName()).log(Level.SEVERE, "Could not load all Pis", ex);
        } catch (Exception ex) {
            Logger.getLogger(AllPis.class.getName()).log(Level.SEVERE, "Could not load all Pis", ex);
        }
    }
    
    public static Pi inputNewPi(Window parent) {
        Pi newPi=Pi.inputNew(parent);
        if (newPi!=null) {
            allPis.add(newPi);
        }
        return(newPi);
    }
    
    public static PisList getAllPis() {
        PisList res=null;
        try {
            res= (PisList) allPis.clone();
        } catch (CloneNotSupportedException ex) {
            Logger.getLogger(AllPis.class.getName()).log(Level.SEVERE, null, ex);
        }
        return(res);
    }
}
